package com.fyt.service.Impl.dictionary.specific;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fyt.controller.utils.Code;
import com.fyt.controller.utils.Result;
import com.fyt.entity.Animalvariety;
import com.fyt.mapper.AnimalvarietyMapper;
import com.fyt.service.dictionary.specific.AnimalvarietyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fyt
 * @since 2023-12-20
 */
@Service
public class AnimalvarietyServiceImpl extends ServiceImpl<AnimalvarietyMapper,Animalvariety> implements AnimalvarietyService {
    @Autowired
    private AnimalvarietyMapper animalvarietyMapper;

    @Override
    public Result addName(Map<String, String> map) {
        String dictionary = map.get("dictionary");
        String name = map.get("name");
        QueryWrapper<Animalvariety> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        List<Animalvariety> animalvarieties = animalvarietyMapper.selectList(queryWrapper);
        if(!animalvarieties.isEmpty()){
            return new Result(null,Code.save_err,"此品种已存在");
        }
        Animalvariety animalvariety = new Animalvariety();
        animalvariety.setDictionaryId(Integer.parseInt(dictionary));
        animalvariety.setEnable("0");
        animalvariety.setName(name);
        int i = animalvarietyMapper.insert(animalvariety);
        return new Result(null,i>0? Code.save_ok:Code.save_err,i>0?"success":"error");
    }

    @Override
    public Result updateEnable(Map<String, String> map) {
        String dictionaryId = map.get("dictionary");
        String id = map.get("id");
        String is_stop = map.get("is_stop"); //是否是禁用
        String enable="true".equals(is_stop)?"1":"0";
        Animalvariety animalvariety = new Animalvariety();
        animalvariety.setEnable(enable);
        animalvariety.setDictionaryId(Integer.parseInt(dictionaryId));
        animalvariety.setId(Integer.parseInt(id));
        int i = animalvarietyMapper.updateById(animalvariety);
        return new Result(null,i>0?Code.update_ok:Code.update_err,i>0?"success":"error");
    }
    @Override
    public Result getList(String id) {
        LambdaQueryWrapper<Animalvariety> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Animalvariety::getDictionaryId,id);
        List<Animalvariety> animalvarieties = animalvarietyMapper.selectList(queryWrapper);
        return new Result(animalvarieties,Code.get_ok,null);
    }


    @Override
    public Result getListByMap(Map<String, String> map){
        String name = map.get("name");
        String dictionary = map.get("dictionary");
        String enable = map.get("enable");
        System.out.println(dictionary);
        LambdaQueryWrapper<Animalvariety> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Animalvariety::getName,name);
        queryWrapper.eq(Animalvariety::getDictionaryId,dictionary);
        if(!"all".equals(enable)){
            queryWrapper.eq(Animalvariety::getEnable,enable);
        }
        List<Animalvariety> animalvarieties = animalvarietyMapper.selectList(queryWrapper);
        System.out.println(animalvarieties);
        return new Result(animalvarieties,Code.get_ok,null);
    }

    @Override
    public Integer getIdByName(String name) {
        QueryWrapper<Animalvariety> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select(Animalvariety::getId).eq(Animalvariety::getName,name);
        Animalvariety animalvariety = animalvarietyMapper.selectOne(queryWrapper);
        return animalvariety.getId();
    }


}
